
//  KCPatrolRepPhotoCell.swift
//  MyKC
//
//  Created by yy on 2017/3/30.
//  Copyright © 2017年 com.yasuo. All rights reserved.
//

import UIKit
import YYWebImage

@objc protocol KCPatrolRepPhotoCellDelegate: NSObjectProtocol {
    @objc optional func photoItemDidClick(item: NSInteger,indexPath: IndexPath)
}

let photoMargin:CGFloat = 10
let photoWidth = (ScreenWidth - photoMargin * 5) / 4
class KCPatrolRepPhotoCell: UITableViewCell {
    @IBOutlet weak var flowLayout: UICollectionViewFlowLayout!
    @IBOutlet weak var collectionView: UICollectionView!
    @IBOutlet weak var collectionH: NSLayoutConstraint!
    weak var delegate: KCPatrolRepPhotoCellDelegate?
//    lazy var imageArray = [UIImage]()
//    lazy var urlImageArray = [String]()
    lazy var index:IndexPath = IndexPath()
    var questionModel: KCQuestionModel? {
        //外界已经完成了的赋值
        didSet {
            if questionModel?.Array == nil {
                questionModel?.Array = [String]()
            }
            if questionModel?.imageArray == nil {
                questionModel?.imageArray = [UIImage]()
            }
            //根据两个数组中图片个数来确定行高
            let count = (questionModel?.Array?.count)! + (questionModel?.imageArray?.count)!
            let row = (count) / 4 + 1
            //里面一层需要手动计算行高----外面一层使用的自动布局行高的方法-------------------
            //这个地方不使用模型记录行高的原因是因为每次都是自己想让它每次都去重新计算---------
            collectionH.constant = CGFloat(row) * photoWidth + CGFloat(row) * photoMargin
            collectionView.reloadData()
        }
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
        flowLayout.itemSize = CGSize(width: photoWidth, height: photoWidth)
        flowLayout.minimumLineSpacing = 10
        flowLayout.minimumInteritemSpacing = 10
        flowLayout.sectionInset = UIEdgeInsetsMake(0, 10, 10, 10);

        collectionView.register(UINib(nibName: "KCPhotoCell", bundle: nil), forCellWithReuseIdentifier: "KCPhotoCellId")
        collectionView.delegate = self
        collectionView.dataSource = self
        collectionView.backgroundColor = UIColor.white
    }
}

// MARK: -  代理方法
extension KCPatrolRepPhotoCell :UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout {
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1
    }
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return ((questionModel?.Array?.count) ?? 0) + (questionModel?.imageArray?.count ?? 0) + 1
    }
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cellPhoto = collectionView.dequeueReusableCell(withReuseIdentifier: "KCPhotoCellId", for: indexPath) as! KCPhotoCell
        if indexPath.item == 0 {
            cellPhoto.imageView.image = UIImage(named: "拍照")
            return cellPhoto
        }
        
        //走到下面说明不是第一个
        if questionModel?.Array?.count != 0 {
            if indexPath.item < (questionModel?.Array?.count)! + 1{
                let urlStr = questionModel?.Array?[indexPath.item - 1]
                guard let urlString = urlStr,let url = URL(string: urlString),let placeImage = UIImage(named: "拍照") else {
                    cellPhoto.imageView.image = UIImage(named: "拍照")
                    return cellPhoto
                }
                cellPhoto.imageView.yy_setImage(with: url, placeholder: placeImage)
            }
            //图片数组中的数据
            else {
                let image = (questionModel?.imageArray)![indexPath.item - 1 - (questionModel?.Array)!.count]
                cellPhoto.imageView.image = image
            }
        }
        else {
            let image = (questionModel?.imageArray)![indexPath.item - 1]
            cellPhoto.imageView.image = image
        }
        return cellPhoto
    }
    
    // MARK: -  点击某一个图标时候调用
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        //点击第一张
        delegate?.photoItemDidClick?(item: indexPath.item, indexPath: index)
    }
 }
